<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<gio-table-wrapper
  [disableSearchInput]="true"
  [length]="total"
  [filters]="tableWrapperFilters"
  (filtersChange)="tableWrapperFilterChange($event)"
>
  <table mat-table [dataSource]="audits" class="audits-table" id="auditsTable" aria-label="Audits table">
    <!-- Date Column -->
    <ng-container matColumnDef="date">
      <th mat-header-cell *matHeaderCellDef id="date">Date</th>
      <td mat-cell *matCellDef="let element">{{ element.createdAt | date: 'medium' }}</td>
    </ng-container>

    <!-- User Column -->
    <ng-container matColumnDef="user">
      <th mat-header-cell *matHeaderCellDef id="user">User</th>
      <td mat-cell *matCellDef="let element">
        {{ element.user.displayName }}
      </td>
    </ng-container>

    <!-- Event Column -->
    <ng-container matColumnDef="event">
      <th mat-header-cell *matHeaderCellDef id="event">Event</th>
      <td mat-cell *matCellDef="let element">{{ element.event }}</td>
    </ng-container>

    <!-- Targets Column -->
    <ng-container matColumnDef="targets">
      <th mat-header-cell *matHeaderCellDef id="targets">Target</th>
      <td mat-cell *matCellDef="let element">
        <div class="targets" *ngFor="let target of element.properties">
          <div class="targets__target">{{ target.key }}:</div>
          <div>{{ target.name }}</div>
        </div>
      </td>
    </ng-container>

    <!-- Patch Column -->
    <ng-container matColumnDef="patch">
      <th mat-header-cell *matHeaderCellDef id="patch">Patch</th>
      <td mat-cell *matCellDef="let element">
        <div class="pre-wrapper">
          <div>
            <button
              *ngIf="!element.displayPatch"
              (click)="element.displayPatch = true"
              mat-icon-button
              aria-label="Button to show patch"
              matTooltip="Show patch"
            >
              <mat-icon svgIcon="gio:eye-empty"></mat-icon>
            </button>

            <button
              *ngIf="element.displayPatch"
              (click)="element.displayPatch = false"
              mat-icon-button
              aria-label="Button to hide patch"
              matTooltip="Hide patch"
            >
              <mat-icon svgIcon="gio:cancel"></mat-icon>
            </button>
          </div>
          <div>
            <pre *ngIf="element.displayPatch">{{ JSON.parse(element.patch) | json }}</pre>
          </div>
        </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

    <!-- Row shown when there is no data -->
    <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
      <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">No audit</td>
    </tr>
  </table>
</gio-table-wrapper>
